package com.minimalist.basic.controller.api;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.minimalist.basic.service.FileService;
import com.minimalist.common.entity.basic.vo.file.FileUpdateStatusVO;
import com.minimalist.common.entity.basic.vo.file.FileUploadRespVO;
import com.minimalist.common.entity.basic.vo.file.FileUploadVO;
import com.minimalist.common.entity.basic.vo.file.FileVO;
import com.minimalist.common.response.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author 11987
 */
@Validated
@RestController
@Tag(name = "对外API接口")
@RequestMapping("/basic/api")
public class ApiFileController{

    @Autowired
    private FileService fileService;

    @GetMapping("/getFileByFileId/{fileId}")
    @Operation(summary = "根据文件ID查询文件信息")
    public R<FileVO> getFileByFileId(
            @PathVariable("fileId") @NotNull(message = "文件ID不能为空")
            @Parameter(name = "fileId", required = true, description = "文件ID")
            Long fileId){
        return R.ok(fileService.getFileListByFileIdsWithoutTenant(
                List.of(fileId)).getFirst());
    }

    @PostMapping(value = "/uploadFile", consumes = "multipart/form-data")
    @Operation(summary = "单文件上传")
    public R<FileUploadRespVO> uploadFile(
            @RequestPart("file") MultipartFile file,
            @RequestParam(value = "fileSource") @Validated Integer fileSource,
            @RequestParam(value = "createId") @Validated Long createId,
            @RequestParam(value = "updateId") @Validated Long updateId,
            @RequestParam(value = "tanantId") @Validated Long tanantId){
        // 创建FileUploadVO对象
        FileUploadVO fileUploadVO = new FileUploadVO();
        fileUploadVO.setFile(file);
        fileUploadVO.setFileSource(fileSource);
        fileUploadVO.setCreateId(createId);
        fileUploadVO.setUpdateId(updateId);
        fileUploadVO.setTanantId(tanantId);
        return R.ok(fileService.uploadFileWithoutTenant(fileUploadVO));
    }

    @GetMapping("getFileListByFileIds")
    @Operation(summary = "根据文件ID列表查询文件信息")
    public R<List<FileVO>> getFileListByFileIds(
            @RequestParam("fileIds") @NotNull(message = "文件ID列表不能为空")
            @Parameter(name = "fileIds", required = true, description = "文件ID列表")
            List<Long> fileIds){
        return R.ok(fileService.getFileListByFileIdsWithoutTenant(fileIds));
    }

    @PostMapping("/updateStatusByFileUrl")
    @Operation(summary = "根据文件URL更新文件状态")
    public R<Void> updateStatusByFileUrl(
            @Validated FileUpdateStatusVO fileUpdateStatusVO){
        fileService.updateStatusByFileUrl(fileUpdateStatusVO);
        return R.ok();
    }

    @DeleteMapping("/deleteFile")
    @SaCheckPermission("basic:file:delete")
    @Operation(summary = "单文件删除")
    public R<Void> deleteFile(
            @RequestParam("fileId") @NotNull(message = "文件ID不能为空")
            @Parameter(name = "fileId", required = true, description = "fileId")
            Long fileId){
        fileService.deleteFileWithoutTenant(fileId);
        return R.ok();
    }

    @DeleteMapping("/deleteBatchFiles")
    @SaCheckPermission("basic:file:delete")
    @Operation(summary = "多文件删除")
    public R<Void> deleteBatchFiles(
            @RequestParam("fileIds") @NotNull(message = "文件ID不能为空")
            @Parameter(name = "fileIds", required = true, description = "fileIds")
            List<Long> fileIds){
        fileService.deleteBatchFilesWithoutTenant(fileIds);
        return R.ok();
    }
}
